home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5957 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Functions as parameters
  5. Date: 22 Feb 1996 00:32:39 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb21173239@qcd.lanl.gov>
  8. References: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: clgonsal@undergrad.math.uwaterloo.ca's message of Wed, 21 Feb 1996 16:40:53 GMT
  13.  
  14. In article <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  15. clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves) writes:
  16. <snip>
  17.    I've often seen and even written code that uses function pointers like
  18.    this:
  19. <snip>
  20.    void foo( int (*func)( int ) ){
  21. <snip>
  22.    void foo( int func( int ) ){
  23. <snip>
  24.    I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
  25.    worked. So my question is: is this standard, or is this some weird compiler
  26.    extension. I've never seen this syntax before. Does it mean the same thing
  27.    as the first piece of code? The body of the two functions is identical, and
  28.    they're called in the same way.
  29.  
  30. Yes, this is standard and they mean the same thing. In a parameter
  31. list:
  32. 1) an array is treated as if it were a pointer to the base type instead.
  33. 2) a function is treated as if it were a pointer to a function instead. 
  34.  
  35.    If they are identical, why is the (*func)(int) syntax so much more common?
  36.  
  37. Because it is indeed a pointer (thus, sizeof(func) is allowed in both
  38. cases: if func were a function, it would naturally be disallowed): C
  39. programmers often don't like hiding behind syntactical
  40. conveniences. There are historical reasons as well.
  41.  
  42.    The func(int) syntax does seem easier to type as well as read, but I've 
  43.    never seen it before.
  44.  
  45. It depends on whether you think in terms of calling the function with
  46. a function parameter and hence write foo(f), or you think of it as
  47. calling the function with a pointer to another function and write it
  48. as foo(&f). (A function name always decays to a pointer to function
  49. unless an operand of sizeof or &: so the two forms of calling are
  50. likewise equivalent.) 
  51.  
  52. Use whichever form you prefer ... but follow something consistently.
  53.  
  54. Cheers
  55. Tanmoy
  56. --
  57. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  58. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  59. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  60. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  61. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  62. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  63.